// Copyright 2009 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Author: Jeff Stewart
//
// The final screen of the wizard.  Lays out any probe faults found and tells
// the user about the status of reporting.

#include <QtGui/QVBoxLayout>
#include <QtGui/QLabel>

#include "ui/thanksscreen.h"
#include "ui/browser.h"
#include "ui/support.h"
#include "util/provider.h"

namespace popdiag {

ThanksScreen::ThanksScreen(QWidget *parent)
  : QWizardPage(parent),
    completed_label_(new QLabel(this)),
    issues_label_(new QLabel(this)),
    details_(new Browser(this, QSize(400, 150))),
    log_link_(new Browser(this, QSize(400, 40))),
    text_report_filepath_(), probes_() {
}

ThanksScreen::~ThanksScreen() {
}

void ThanksScreen::SetupUI() {
  const QString email = wizard()->field("email").toString();
  QGridLayout *layout = new QGridLayout(this);
  QLabel *title = new QLabel(this);
  QString title_text = "<h1>";
  title_text += tr("Step 2/2: Results", "Step 4 of the wizard");
  title_text += "</h1><p></p>";
  title->setText(title_text);
  title->setWordWrap(true);
  layout->addWidget(title, 0, 1);
  layout->addWidget(completed_label_, 1, 1);
  layout->addWidget(issues_label_, 2, 1);
  layout->addWidget(details_, 3, 1);
  layout->addWidget(log_link_, 4, 1);
}

void ThanksScreen::UpdateFaults() {
  QString email = wizard()->field("email").toString();
  QString completed_text;
  completed_text += tr("Tests on %1 complete!",
             "%1 is the user's email address").arg(email);
  QList<Probe *> failing_probes;
  for (ProbeSet::Storage::iterator i = probes_.begin();
       i != probes_.end(); ++i) {
    Probe *probe = *i;
    ResultStatus status = probe->result_status();
    if (status == PROBE_FAILED) {
      failing_probes.push_back(probe);
    }
  }
  QString issues_text;
  QString details_text;
  if (failing_probes.isEmpty()) {
    if (provider->HasSupportLinks()) {
      issues_text += tr("No issues were found, but we can still help!",
                        "Offers encouragement for a resolution");
      details_text += tr("Please give us %1additional information%2 "
                        "and we will do our best to fix your problem.",
                "Prompts the user to click a link to continue diagnosis. "
                "%1 and %2 are hyperlink boundaries.")
          .arg("<a href=\"" +
               provider->GetSupportLink(Support::NO_PROBE_FAULTS_FOUND) +
               "\">")
          .arg("</a>");
    } else {
      issues_text += tr("No issues were found.");;
    }
  } else {
    issues_text += tr("The following issues were found:",
                      "A section header before a bulleted list "
                      "of problems found.");
    details_text += "<ol>";
    for (QList<Probe *>::iterator i = failing_probes.begin();
         i != failing_probes.end(); i++) {
      Probe *probe = *i;
      details_text += "<li>" + probe->GetDisplayText();
      if (provider->HasSupportLinks()) {
        details_text += " <a href=\"" +
            provider->GetProbeLink(probe->GetProbeId(), probe->GetFaultId()) +
            "\">";
        details_text += tr("Help me fix this issue",
                           "Hyperlink label listed after each fault found");
        details_text += "</a>";
      }
    }
    details_text.append("</ol>");
  }
  QString log_link_text;
  QByteArray exclude;
  exclude.append('/');
  QString url = "file:///"
                + QString::fromAscii(
                    QUrl::toPercentEncoding(text_report_filepath_, exclude));
  QString open_anchor = QString("<a href=\"%1\">").arg(url);
  log_link_text += "<p>";
  log_link_text += tr("We've placed a file on your desktop detailing "
                      "what we found.",
                      "Explains that we put a file on their desktop");
  log_link_text += "<br>";
  log_link_text += tr("Click %1here%2 to open the log file.",
                      "A hyperlink to open a log file in a browser. "
                      "%1 and %2 are hyperlink boundaries.")
                      .arg(open_anchor).arg("</a>");
  log_link_text += "</p>";
  // Unfortunately we have to decide about the size of the details
  // field before we know how much text it contains. If there were
  // no errors, it will contain little text, and there will be a lot
  // of space until the log_link field begins. To avoid a weird
  // layout, add the log link to the details field in this case.
  if (failing_probes.isEmpty()) {
    details_text += log_link_text;
    log_link_text = "";
  }
  completed_label_->setText(completed_text);
  issues_label_->setText(issues_text);
  details_->setHtml(details_text);
  log_link_->setHtml(log_link_text);
}

void ThanksScreen::ReportSaved(const QString &path) {
  text_report_filepath_ = path;
}

void ThanksScreen::cleanupPage() {
  text_report_filepath_.clear();
  probes_.clear();
}

void ThanksScreen::initializePage() {
  wizard()->setOption(QWizard::NoCancelButton, true);
  wizard()->setOption(QWizard::NoBackButtonOnLastPage, true);
  setFinalPage(true);
}

void ThanksScreen::DisplayFaults(const ProbeSet::Storage &probes) {
  probes_ = probes;
  UpdateFaults();
}


}  // namespace popdiag
